package com.set.mobileapp.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MobileAppBizUtil {
	public static final SimpleDateFormat ymdhms = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static List<Map> queryCutOffPlan(Context ctx, Date bg, Date ed,
			String comp) {
		List rslt = new ArrayList();
		InputStream is = null;
		OutputStream os = null;
		Long bgL = bg.getTime();
		Long edL = ed.getTime();
		try {
			os = new ByteArrayOutputStream();
			is = ctx.getAssets().open("cutoff_data.txt");
			byte[] b = new byte[1024];
			int len = -1;
			while ((len = is.read(b)) != -1) {
				os.write(b, 0, len);
			}
			String l = os.toString();
			String[] rows = l.split("==");
			for (String r : rows) {
				String[] attrs = r.split(",");
				String stime = attrs[4];
				if (stime != null && !stime.equals("")) {
					try {
						Date sendTime = ymdhms.parse(stime);
						Long sdL = sendTime.getTime();
						String compnew = attrs[16];
						if (sdL >= bgL && sdL < edL) {
							Map m = new HashMap();
							String id = attrs[7];
							String reason = attrs[3];
							String bgTime = attrs[1];
							String edTime = attrs[2];
							String region = attrs[10];
							String peason = attrs[6];
							String desc = attrs[15];
							m.put("TITLE", id);
							m.put("REASON", reason);
							m.put("BG_TIME", bgTime);
							m.put("ED_TIME", edTime);
							m.put("REGION", region);
							m.put("TIME", stime);
							m.put("SENDER", peason);
							m.put("DESC", desc);
							rslt.add(m);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			MobileAppUtils.closeIs(is);
			MobileAppUtils.closeOs(os);
		}
		return rslt;
	}

	public static List query4PlanCutOff(String sql, Context ctx) {
		List rslt = new ArrayList();
		//sql = "select * from xn_cutoff_notice";
		SQLiteDatabase db = null;
		Cursor c = null;
		try {
			db = ctx.openOrCreateDatabase("spi.db", Context.MODE_PRIVATE, null);
			c = db.rawQuery(sql, null);
			
			while (c.moveToNext()) {
				Map m = new HashMap();
				m.put("TITLE", c.getString(c.getColumnIndex("ID")));
				m.put("REASON", c.getString(c.getColumnIndex("CUTOFF_REASON")));

				m.put("BG_TIME", c.getString(c.getColumnIndex("CUTOFF_B_TIME")));
				m.put("ED_TIME", c.getString(c.getColumnIndex("CUTOFF_E_TIME")));
				m.put("REGION", c.getString(c.getColumnIndex("TDFW")));
				m.put("TIME", c.getString(c.getColumnIndex("NOTICE_SEND_TIME")));
				m.put("SENDER", c.getString(c.getColumnIndex("CUTOFF_SENDER")));
				m.put("DESC", c.getString(c.getColumnIndex("WORK_CONTENT")));
				m.put("DEV_NAME", c.getString(c.getColumnIndex("SBMC")));
				rslt.add(m);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MobileAppUtils.closeDB(db);
			MobileAppUtils.closeCursor(c);
		}
		return rslt;
	}

	public static Map loadKpi(Context ctx) {
		Map m = new HashMap();
		InputStream is = null;
		try {
			is = ctx.getAssets().open("kpidata.txt");
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader input = new BufferedReader(isr);
			String s = input.readLine();
			while (s != null) {
				String[] attrs = s.split(",");
				String ymd = attrs[0].split("=")[1];
				List lst = (List) m.get(ymd);
				if (lst == null) {
					lst = new ArrayList();
					m.put(ymd, lst);
				}
				Map subMap = new HashMap();
				String value = attrs[2].split("=")[1];
				String time = attrs[5].substring(5, attrs[5].length() - 1);
				// time = time.split("=")[1];
				while (time.length() < 6) {
					time = "0" + time;
				}
				subMap.put("VALUE", Double.valueOf(value));
				subMap.put("TIME", time);
				lst.add(subMap);
				s = input.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return m;
	}
}
